const http = require('http'),
           url=require('url'),
                 fs = require('fs');

let data = require('./data.js');

   
let chapterList=data.chapterList;
chapterList.sort((a,b)=>{
    let time1=new Date(a.publishTimer);
      let time2=new Date(b.publishTimer);

        return time2-time1;

});

let  html=fs.readFileSync('./chapters.html');

http.createServer((req, res) => {
    var urls=url.parse(req.url,true);
     
    if(urls.pathname==='/'){
          res.writeHead(200, {'Content-Type': 'text/html; charset=utf-8'});
              res.end(html);
                
    }else{
      fs.readFile('.'+req.url,(err,data)=>{
        if(err){
                  console.error(err);
                        
        }else{
                  res.end(data);
                        
        }
            
      });
        
    }
    if(req.url==='/getlist'){
          res.end(JSON.stringify(chapterList));
              return;
                
    }
     
}).listen(8080);
